package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = MenuService.class)
@Transactional
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuDao menuDao;
    //分页查询
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        if (pageSize == null || pageSize < 1) {
            pageSize = 10;
        }
        if (queryString == null || queryString.trim().equals("")) {
            queryString = "";
        }
        PageHelper.startPage(currentPage, pageSize);

        Page<Menu> page = menuDao.findByCondition("%" + queryString.trim() + "%");
        for (Menu menu : page) {
            List<Menu> children = menuDao.findByParentId(menu.getId());
            menu.setChildren(children);
        }
        return new PageResult(page.getTotal(), page.getResult());
    }

    //查询父菜单
    @Override
    public List<Menu> findParent() {

        return menuDao.findParent();
    }

    //新增菜单
    @Override
    public Integer add(Menu menu) {

        //判断菜单名称是否重复
        Integer nameCount = menuDao.findCountByName(menu.getName());
        if (nameCount >0){
            throw new RuntimeException("菜单名称已存在");
        }
        //判断访问路径是否重复
        Integer linkUrlCount = menuDao.findCountByLinkUrl(menu.getLinkUrl());
        if (linkUrlCount >0){
            throw new RuntimeException("访问路径不能重复");
        }
        //判断路径级别是否重复
        Integer pathCount = menuDao.findCountByPath(menu.getPath());
        if (pathCount >0){
            throw new RuntimeException("路径级别不能重复");
        }

        return menuDao.add(menu);
    }

    //查询单个菜单
    @Override
    public Menu findById(Integer id) {


        Menu menu = menuDao.findById(id);
        Integer parentMenuId = menu.getParentMenuId();
        //如果有父节点
        if (parentMenuId !=null){
            menu = menuDao.findByChildId(menu.getId());
        }
        //没有父节点直接返回
        return menu;
    }

    //编辑菜单
    @Override
    public Integer edit(Menu menu) {

        Integer count = menuDao.edit(menu);
        return count;
    }

    //删除菜单
    @Override
    public Integer deleteById(Integer id) {
        //首先查询有没有关联角色
        Integer count = menuDao.findRoleByMenuId(id);
        if (count > 0){
            throw new RuntimeException("该菜单关联角色，请核对后删除");
        }

        Menu menu = menuDao.findById(id);
        //如果为父菜单 查询是否存在子菜单
        Integer deleteCount = 0;
        if (menu.getParentMenuId()==null){
            List<Menu> menuList = menuDao.findByParentId(menu.getId());
            if (menuList.size()>0){
                throw new RuntimeException("该菜单为父菜单，请删除子菜单后再删除");
            }
            else {
                deleteCount = menuDao.deleteById(id);
            }
        }
        deleteCount = menuDao.deleteById(id);
        return deleteCount;
    }


}
